package com.hyyt.shopjsp.product.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.product.pojo.*;
import com.hyyt.shopjsp.product.service.*;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.tree.NodeUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author 刘超 on 2017/7/27.
 */

@Controller
@RequestMapping("/back/productType")
public class ShopProductTypeController extends BaseController {

    private Logger logger = Logger.getLogger(ShopProductTypeController.class);

    @Resource
    private IShopProductTypeService shopProductTypeService;
    @Resource
    private IShopProductInfoService shopProductInfoService;
    @Resource
    private IShopProductMeasuringUnitService shopProductMeasuringUnitService;
    @Resource
    private IShopBrandTypeService shopBrandTypeService;

    /**
     * 跳转到商品分类列表页
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoProductTypePage")
    public String gotoProductTypePage(){
        return "/back/shop/product/shopProductType/productType/productTypeList";
    }

    /**
     * 根据id展示左侧商品树形结构
     * @param response
     */
    @RequestMapping("getNodes")
    public void getNodes(String opType,HttpServletResponse response){
        if(Utils.objectIsNotEmpty(opType)){
            Map<String, Object> param = new HashMap<>();
            String [] selectColmunsArray={"productTypeId","sortName","parentId","isLeaf"};
            param.put("selectColmunsArray",selectColmunsArray);
            param.put("sortColumns", "sortCode asc");
            List<ShopProductType> typeList=null;
            if("manager".equals(opType)){//查询树自身维护的一级及二级节点
                typeList=shopProductTypeService.getNodesForManager(param);
            }else if("navigate".equals(opType)){//查询树关联数据维护的所有节点
                typeList=shopProductTypeService.getNodesForNavigate(param);
            }else{
                ResultMsg.create(false,"树加载失败！");
            }
            List nodeList = new ArrayList();
            HashMap nodeMap = new HashMap();
            for(int i = 0; i < typeList.size(); i++) {
                ShopProductType spt = typeList.get(i);
                nodeMap = new HashMap();
                nodeMap.put("id",spt.getProductTypeId());
                nodeMap.put("text", spt.getSortName());
                nodeMap.put("isLeaf", spt.getIsLeaf());
                String parentId = String.valueOf(spt.getParentId());
                if (Utils.objectIsEmpty(parentId)) {
                    parentId = "0";
                }
                nodeMap.put("parentId",parentId);
                nodeList.add(nodeMap);
            }
            //获取分类的json字符串信息
            String jsonInfo= NodeUtil.getNodeJson(nodeList,"商品分类");
            ResultMsg.create(jsonInfo, response, false);
        }else{
            ResultMsg.create(false,"树加载失败！");
        }

    }

    /**
     * 查询指定分类下的子分类列表
     *
     * @param page
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "listSubCategoryByParentId", method = RequestMethod.POST)
    @ResponseBody
    public void listSubCategoryByParentId(Integer parentId, String sortName, Page page, HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(parentId)) {
            param.put("parentId", String.valueOf(parentId));
        }else{//默认为所有一级节点
            param.put("parentId","0");
        }
        if (Utils.objectIsNotEmpty(sortName)) {
            param.put("sortName", sortName);
        }
        int totalRecordCount = shopProductTypeService.countShopProductType(param);
        //定义查询的字段
        String [] selectColmunsArray={"productTypeId","sortName","parentId","sortCode"};
        param.put("selectColmunsArray",selectColmunsArray);
        param.put("sortColumns", "sortCode asc");
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<Map<String, Object>> list = shopProductTypeService.queryShopProductTypeBySelf(param, page.getPageRecordBeginIndex(), page.getPageSize());
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", list);// rows键 存放每页记录 list
        param.put("parentId", parentId);
        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 查看商品分类对象
     * @param productTypeId
     * @return
     */
    @RequestMapping(value = "getProductTypeObject",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getProductTypeObject(Integer productTypeId){
        if(Utils.objectIsNotEmpty(productTypeId)){
            ShopProductType shopProductType = shopProductTypeService.loadShopProductType(productTypeId);
            return ResultMsg.create(true,null, FastJsonUtils.toJSONString(shopProductType));
        }
        return ResultMsg.create(false);
    }

    /**
     * 保存或更新分类对象
     * @param shopProductType 分类对象
     * @return
     */
    @RequestMapping(value="saveOrEditProductType",method=RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrEditProductType(ShopProductType shopProductType){
        return shopProductTypeService.saveOrUpdateShopProductType(shopProductType);
    }

    /**
     * 根据id删除商品分类
     * @param productTypeId
     * @return
     */
    @RequestMapping(value="deleteProductType",method=RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteProductType(Integer productTypeId){
        return shopProductTypeService.deleteShopProductType(productTypeId);
    }

    /**供外部类功能使用的方法**/

    /**
     * 查询指定分类下的子分类列表-供非分页功能使用
     *
     * @param parentId 父分类ID
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "getSubCategoryListByParentId", method = RequestMethod.POST)
    @ResponseBody
    public void getSubCategoryListByParentId(Integer parentId,HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(parentId)) {
            param.put("parentId", String.valueOf(parentId));
        }else{//默认为所有一级节点
            param.put("parentId","0");
        }
        //定义查询的字段
        String [] selectColmunsArray={"productTypeId","sortName","parentId","sortCode","levelCode"};
        param.put("selectColmunsArray",selectColmunsArray);
        param.put("sortColumns", "sortCode asc");
        List<ShopProductType> sptList = shopProductTypeService.queryShopProductType(param);
        param.clear();
        param.put("sptList", sptList);
        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 根据商品分类ID获得商品信息列表
     * @param productTypeId 商品分类id
     * @param page
     * @param productFullName 商品分类
     * @param response
     */
    @RequestMapping(value = "listProductInfoByProductTypeId",method = RequestMethod.POST)
    @ResponseBody
    public void listProductInfoByProductTypeId(Integer productTypeId,String productFullName,Page page,HttpServletResponse response){
        Map<String, Object> params = new HashMap<>();
        if(Utils.objectIsNotEmpty(productTypeId)){
            params.put("productTypeId",productTypeId);
        }
        if(Utils.objectIsNotEmpty(productFullName.trim())){
            params.put("productFullName",productFullName);
        }
        int totalRecordCount = shopProductInfoService.countShopProductInfo(params);
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<Map<String,Object>> shopProductInfoList = shopProductInfoService.queryShopProductInfo(params,page.getPageRecordBeginIndex(),page.getPageSize());
        params.clear();
        params.put("total",totalRecordCount);
        params.put("rows",shopProductInfoList);
        ResultMsg.create(params,response);
    }


    /**
     * 通过商品分类ID获取对应的品牌列表
     *
     * @param productTypeId 商品分类ID
     * @param response
     */
    @RequestMapping(value = "getBrandListByProductTypeId",method = RequestMethod.POST)
    @ResponseBody
    public void getBrandListByProductTypeId(Integer productTypeId,HttpServletResponse response){
        List<Map<String,Object>> shopBrandList =shopBrandTypeService.queryBrandListByProductTypeId(productTypeId);
        Map<String, Object> params = new HashMap<>();
        params.put("shopBrandList",shopBrandList);
        ResultMsg.create(params,response);
    }

    /**
     * 通过商品分类ID获取对应的品牌列表
     *
     * @param productTypeId 商品分类ID
     * @param response
     */
    @RequestMapping(value = "getMeasuringUnitListByProductTypeId",method = RequestMethod.POST)
    @ResponseBody
    public void getMeasuringUnitListByProductTypeId(Integer productTypeId,HttpServletResponse response) {
        List<Map<String,Object>> measuringUnitList =shopProductMeasuringUnitService.queryMeasuringUnitListByProductTypeId(productTypeId);
        Map<String, Object> params = new HashMap<>();
        params.put("measuringUnitList",measuringUnitList);
        ResultMsg.create(params,response);
    }

}
